package cn.change360.newappchildrenenglish.adapter;

import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.os.Build;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;


import java.util.List;

import cn.change360.newappchildrenenglish.R;
import cn.change360.newappchildrenenglish.mode.Columns;
import cn.change360.newappchildrenenglish.utils.FileUtil;

/**
 * Created by Jacky on 2015/10/22.
 */

public class ColumnsAdapter extends BaseAdapter {

    private LayoutInflater mInflater;
    private List<Columns> datas;
    private Context context;


    public ColumnsAdapter(Context context, List<Columns> datas) {
        this.mInflater = LayoutInflater.from(context);
        this.datas = datas;
        this.context = context;
    }

    @Override
    public int getCount() {
        return datas.size();
    }

    @Override
    public Object getItem(int arg0) {
        return datas.get(arg0);
    }

    @Override
    public long getItemId(int arg0) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = mInflater.inflate(R.layout.adapter_columns, null);
            holder.column_title = (TextView) convertView.findViewById(R.id.column_title);
            holder.column_ico = (ImageView) convertView.findViewById(R.id.column_ico);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.column_title.setText(datas.get(position).getColumn_title()+ "");
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
            holder.column_ico.setBackground(new BitmapDrawable(FileUtil.getImageFromAssetsFile(context, "ico/ico_" + datas.get(position).getId() + ".png")));
        } else {
            holder.column_ico.setImageDrawable(new BitmapDrawable(FileUtil.getImageFromAssetsFile(context, "ico/ico_" + datas.get(position).getId() + ".png")));
        }
        return convertView;
    }

    public final class ViewHolder {
        public TextView column_title;
        public ImageView column_ico;
    }
}